package com.easy.oss.controller;

import com.easy.framework.common.model.R;
import com.easy.framework.common.utils.Func;
import com.easy.oss.model.FileInfoModel;
import com.easy.oss.service.EndpointService;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * Minio对象存储端点
 *
 * @author Yang Huijing
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("oss/endpoint")
public class EndpointController {

    /**
     * 对象存储构建类
     */
    private final EndpointService endpointService;

    /**
     * 获取文件信息
     * @param filename 存储桶对象名称
     * @return
     */
    @SneakyThrows
    @GetMapping("/stat")
    public R<FileInfoModel> statFile(@RequestParam("filename") String filename) {
        return R.success(endpointService.statFile(filename));
    }

    /**
     * 上传文件
     * @param file 文件
     * @return
     */
    @SneakyThrows
    @PostMapping("/upload")
    public R<FileInfoModel> uploadFile(@RequestParam("file") MultipartFile file) {
        return R.success(endpointService.upload(file));
    }


    /**
     * 删除文件
     *
     * @param filename 存储桶对象名称
     * @return R
     */
    @SneakyThrows
    @DeleteMapping("/remove")
    public R removeFile(@RequestParam("filename") String filename) {
        endpointService.remove(filename);
        return R.success("操作成功");
    }

    /**
     * 批量删除文件
     *
     * @param fileNames 存储桶对象名称集合
     * @return R
     */
    @SneakyThrows
    @DeleteMapping("/removes")
    public R removeFiles(@RequestParam String fileNames) {
        endpointService.removeBatch(Func.toStrList(fileNames));
        return R.success("操作成功");
    }
}
